package com.github.postsanf.yinian.activity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.View;

import com.github.postsanf.yinian.BaseActivity;
import com.github.postsanf.yinian.R;
import com.github.postsanf.yinian.adapter.ShopAddressListAdapter;
import com.github.postsanf.yinian.constants.ACTIONs;
import com.github.postsanf.yinian.constants.CommonConstants;
import com.github.postsanf.yinian.constants.YNSingleton;
import com.github.postsanf.yinian.entity.YNAddressResponse;
import com.github.postsanf.yinian.utils.ReqUtils;
import com.litesuits.http.exception.HttpException;
import com.litesuits.http.listener.HttpListener;
import com.litesuits.http.request.StringRequest;
import com.litesuits.http.request.param.HttpMethods;
import com.litesuits.http.response.Response;
import com.umeng.analytics.MobclickAgent;

import java.util.LinkedHashMap;

public class ShopAddressListActivity extends BaseActivity implements View.OnClickListener {
    private RecyclerView receiver_address_list;              // 列表视图
    private ShopAddressListAdapter mAdapter;
    private int userId;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_receiveraddresslist);
        registerBoradcastReceiver();
        initView();
        initAddressList();
    }

    private void initView() {
        userId = application.getCurID();
        findViewById(R.id.addresslist_back).setOnClickListener(this);
        findViewById(R.id.addresslist_add).setOnClickListener(this);
        receiver_address_list = (RecyclerView) findViewById(R.id.receiver_address_list);
        receiver_address_list.setLayoutManager(new LinearLayoutManager(this));
        mAdapter = new ShopAddressListAdapter(ShopAddressListActivity.this, YNSingleton.getInstance().getmAddressList(),userId);
        receiver_address_list.setAdapter(mAdapter);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterReceiver(mListReceiver);
    }

    private void registerBoradcastReceiver() {
        IntentFilter myIntentFilter = new IntentFilter();
        myIntentFilter.addAction(ACTIONs.actionInitShopAddressList);
        myIntentFilter.addAction(ACTIONs.actionUpdateShopAddressList);
        registerReceiver(mListReceiver, myIntentFilter);
    }

    BroadcastReceiver mListReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();
            switch (action) {
                case ACTIONs.actionInitShopAddressList:
                    mAdapter.notifyDataSetChanged();
                    break;
                case ACTIONs.actionUpdateShopAddressList:
                    initAddressList();
                    break;
            }
        }
    };

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.addresslist_back:
                finish();
                break;
            case R.id.addresslist_add:
                intent2Activity(ShopAddressEditActivity.class);
                break;
        }
    }

    private void initAddressList() {
        LinkedHashMap<String, String> mParam = new LinkedHashMap<>();
        mParam.put("userid", String.valueOf(application.getCurID()));
        StringRequest req = new StringRequest(getString(R.string.SHOP_URL) + getString(R.string.ynShowAllAddress))
                .setMethod(HttpMethods.Post)
                .setParamMap(mParam)
                .setHttpListener(new HttpListener<String>(true, false, true) {
                    @Override
                    public void onSuccess(String s, Response<String> response) {
                        YNAddressResponse resAddress = gson.fromJson(s, YNAddressResponse.class);
                        if (ReqUtils.isSuccess(resAddress.getCode())) {
                            YNSingleton.getInstance().getmAddressList().clear();
                            YNSingleton.getInstance().getmAddressList().addAll(resAddress.getData());
                            Intent upIntent = new Intent(ACTIONs.actionInitShopAddressList);
                            sendBroadcast(upIntent);
                        }
                    }

                    @Override
                    public void onFailure(HttpException e, Response<String> response) {
                        showToast(CommonConstants.YN_NET_LINK);
                    }
                });
        liteHttp.executeAsync(req);
    }

    // 友盟统计
    public void onResume() {
        super.onResume();
        MobclickAgent.onPageStart(this.getClass().getSimpleName());
        MobclickAgent.onResume(this);
    }

    public void onPause() {
        super.onPause();
        MobclickAgent.onPageEnd(this.getClass().getSimpleName());
        MobclickAgent.onPause(this);
    }
}
